package yunjiao.javatutorials.guava.base;

import com.google.common.base.Equivalence;

import java.util.HashSet;
import java.util.Set;

/**
 * 基于自定义逻辑的集合操作
 *
 * @author yangyunjiao
 */
public class CaseInsensitiveEquivalence extends Equivalence<String> {
    @Override
    protected boolean doEquivalent(String a, String b) {
        if (a == null && b == null) return true;
        if (a == null || b == null) return false;
        return a.equalsIgnoreCase(b);
    }

    @Override
    protected int doHash(String s) {
        return s == null ? 0 : s.toLowerCase().hashCode();
    }

    public static void main(String[] args) {
        // 使用示例
        Equivalence<String> caseInsensitiveEq = new CaseInsensitiveEquivalence();

        Set<Wrapper<String>> uniqueStrings = new HashSet<>();
        uniqueStrings.add(caseInsensitiveEq.wrap("Hello"));
        uniqueStrings.add(caseInsensitiveEq.wrap("HELLO"));
        uniqueStrings.add(caseInsensitiveEq.wrap("world"));

        System.out.println(uniqueStrings.size()); // 2，"HELLO" 被视为与 "Hello" 等价
    }
}
